﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace NeurOthello.NeurOthelloGui
{
    class BoardPanel : System.Windows.Forms.Panel
    {
        private Pen myPen = new Pen(Color.Black);
        private Logic.Othello game;
        public Logic.Othello Game { set { game = value; } }
        private Logic.HumanPlayer human = new Logic.HumanPlayer();
        public Logic.Player Human { get { return human; } }
        private int[,] prevBoard;

        public BoardPanel()
            : base()
        {
            this.SetStyle(ControlStyles.Selectable, true);
            this.TabStop = true;                        
        }

        protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Get the graphics object 
            Graphics gfx = paintEvnt.Graphics;

            gfx.FillRectangle(new SolidBrush(Color.Green), 0, 0, this.Size.Width, this.Size.Height);
            // Create a new pen that we shall use for drawing the line 
            
            // Loop and create a horizontal line 10 pixels below the last one 
            for (int i = 10; i <= 500; i = i + 60)
            {
                gfx.DrawLine(myPen, 10, i, 490, i);
            }
            // Loop and create a vertical line 10 pixels next to the last one 
            for (int x = 10; x < 500; x = x + 60)
            {
                gfx.DrawLine(myPen, x, 10, x, 490);
            }
            DrawPlayerMarkers(gfx);
        }

        private void DrawPlayerMarkers(Graphics gfx)
        {
            if (game != null)
            {
                int[,] gamestate = game.Gameboard;

                if (prevBoard == null)
                {
                    for (int i = 0; i < 8; i++)
                        for (int j = 0; j < 8; j++)
                            if (gamestate[i, j] != 0)
                                PlaceMarker(gfx, gamestate[i, j], i, j);
                }
                else
                {
                    if (game.LastMove != null) 
                        MarkAsChanged(gfx, game.LastMove.X, game.LastMove.Y, true);
                    for (int i = 0; i < 8; i++)
                        for (int j = 0; j < 8; j++)
                            if (gamestate[i, j] != 0)
                            {                                
                                if (gamestate[i, j] == 3 - prevBoard[i, j])
                                    MarkAsChanged(gfx, i, j, false);                                
                                PlaceMarker(gfx, gamestate[i, j], i, j);
                            }
                }
                prevBoard = gamestate;
            }
        }

        private void PlaceMarker(Graphics gfx, int player, int x, int y)
        {
            int dx = 15 + 60 * x;
            int dy = 15 + 60 * y;
            gfx.FillEllipse(PlayerBrush(player), new Rectangle(dx, dy, 50, 50));
        }

        private void MarkAsChanged(Graphics gfx, int x, int y, bool last)
        {
            int dx = 12 + 60 * x;
            int dy = 12 + 60 * y;
            if (last)
                gfx.FillEllipse(new SolidBrush(Color.Red), new Rectangle(dx, dy, 56, 56));
            else 
                gfx.FillEllipse(new SolidBrush(Color.Gold), new Rectangle(dx, dy, 56, 56));
        }

        public void ClearStates()
        {
            prevBoard = null;
        }

        private Brush PlayerBrush(int player)
        {
            if(player == 1) 
                return new SolidBrush(Color.Black);
            else 
                return new SolidBrush(Color.Ivory);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // BoardPanel
            //             
            this.ResumeLayout(false);

        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.Focus();
            if (game != null && game.PlayerTurn != 0 && !game.CurrentIsAi)
            {
                int x = (e.X - 15) / 60;
                int y = (e.Y - 15) / 60;
                human.enterMove(x, y);                
            }
            base.OnMouseDown(e);
        }

    }
}
